package util

import (
	"golang.org/x/crypto/bcrypt"
)

//HashPassword 对密码 hash
func HashPassword(password string) (string, error) {
	hashPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	return string(hashPassword), nil
}

// VerifyPassword 校验输入的密码和 hash 过的密码是否相同
func VerifyPassword(hashPassword, inputPassword string) error {
	err := bcrypt.CompareHashAndPassword([]byte(hashPassword), []byte(inputPassword))
	if err != nil {
		return err
	}
	return nil
}
